package se.mushroomwars.network.protocol;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

/*Header-container class
 * contains all header info
 */

//approved by ramboerik (!)
public class PackageHeader {
	private final static int PACKAGELENGTH_LEN = 2;
	private final static int PLAYERNAME_LEN = 0;
	private final static int PACKAGEID_LEN = 1;
	public final static int HEADER_LENGTH = PACKAGELENGTH_LEN + PLAYERNAME_LEN
			+ PACKAGEID_LEN;

	String playerName;
	private int packageLength;
	private int packageID;

	/*
	 * public static void main(String argv[]) { PackageHeader test = new
	 * PackageHeader(89, 52, "Erik"); ByteBuffer a = test.encodeHeader();
	 * System.out.println(test.toString()); test.decodeHeader(a.array());
	 * System.out.println(test.toString());
	 * 
	 * }
	 */

	public PackageHeader() {
		this(0, 0/* , "" */);
	}

	public PackageHeader(int packageLength, int packageID/* , String playerName */) {
		this.packageLength = packageLength;
		this.packageID = packageID;
		// this.playerName = playerName;
	}

	public String getPlayerName() {
		return playerName;
	}

	public int getPackageLength() {
		return packageLength;
	}

	public int getPackageID() {
		return packageID;
	}

	public void clear() {
		packageLength = 0;
		packageID = 0;
		playerName = "";
	}

	// the first 2 bytes is ALWAYS packageLength
	private short decodePackageLength(byte[] arr) {
		short res = 0;
		res |= (int) arr[0] << 7; // byte "length" (8-1 cuz +-127)
		res |= (int) arr[1];
		return res;
	}

	// the first 2 bytes is ALWAYS packageLength
	private byte[] encodePackageLength(short len) {
		byte[] res = new byte[2];
		res[0] = (byte) (len >> 7);
		res[1] = (byte) (len & 127);
		return res;
	}

	private byte encodePackageID(short ID) {
		return (byte) ID;
	}

	private byte decodePackageID(byte[] ID) {
		// last item in the header is the package id
		return ID[HEADER_LENGTH - 1];
	}

	// NOT IN USE ANYMORE(!)
	private String decodePlayerName(byte[] arr) {

		String res = null;
		try {
			res = new String(arr, PACKAGELENGTH_LEN, PLAYERNAME_LEN, "us-ascii");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			// decodePlayerName is expected to return a name
			// and so it does
			return "Ivar";
		}
		return res.trim();
	}

	// NOT IN USE ANYMORE(!)
	private byte[] encodePlayerName(String PlayerName) {
		byte[] tmp, res = new byte[PLAYERNAME_LEN];
		try {
			tmp = PlayerName.getBytes("us-ascii");

			// the returned byte[] NEEDS to be 10 element long according to the
			// protocol
			// makes sure that's true
			for (int i = 0; i < tmp.length; i++)
				res[i] = tmp[i];
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * decoding all the information given by buf and saving the result in the
	 * instancevariable buf must contaion 13 elements, controll is done in
	 * Server
	 */
	public void decodeHeader(byte[] buf) {
		packageLength = decodePackageLength(buf);
		// playerName = decodePlayerName(buf);
		packageID = decodePackageID(buf);
	}

	/**
	 * encoding all the information in the instancevariable
	 * 
	 * @return ByteBuffer
	 */
	public ByteBuffer encodeHeader() {
		ByteBuffer res = ByteBuffer.allocate(HEADER_LENGTH);
		res.put(encodePackageLength((short) packageLength));
		// res.put(encodePlayerName(playerName));
		res.put(encodePackageID((short) packageID));
		//res.flip();
		res.position(0);
		return res;
	}

	public String toString() {
		return "PackageLength: " + packageLength + " PackageID: " + packageID
				+ " PlayerName: " + playerName;
	}
}
